package com.justnow.solution11;

public class Solution11 {
    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int maxarea = -1;
        int min;
        int temparea;
        while (left < right) {
            int d = right - left;
            if (height[left] < height[right] ) {
                temparea = d * height[left];
                left++;
            } else {
                temparea = d * height[right];
                right--;
            }
            maxarea = Math.max(maxarea, temparea);
        }

        return maxarea;
    }

    /**
     * 官方解法,代码更加简便
     * @param height
     * @return
     */
    public int maxArea2(int[] height) {
        int left = 0, right = height.length - 1;
        int ans = 0;
        while (left < right) {
            int area = Math.min(height[left], height[right]) * (right - left);
            ans = Math.max(ans, area);
            if (height[left] <= height[right])
                left++;
            else
                right--;
        }
        return ans;
    }
    public static void main(String[] args) {
        Solution11 solution11 = new Solution11();
        int i = solution11.maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7});
        System.out.println(i);

    }
}
